using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Common;
using Microsoft.TeamFoundation.WorkItemTracking;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace Hemi
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Hemi());

            try
            {
                System.Net.NetworkCredential creds = new System.Net.NetworkCredential("darren", "Pass@word1");
                TeamFoundationServer tfs = new TeamFoundationServer("192.168.0.2", creds);

                WorkItemStore workItemStore = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));

                WorkItem srcWorkItem = workItemStore.GetWorkItem(920);

                //
                // First create a copy of the work item based on the destination
                // work item type
                //
                WorkItemType cmmiBug = workItemStore.Projects["ReportSample"].WorkItemTypes["Bug"];
                WorkItem destWorkItem = srcWorkItem.Copy(cmmiBug);

                //
                // See what errors we get
                //
                ArrayList errors = destWorkItem.Validate();
               
                foreach (Field error in errors)                
                {
                    if (error.IsRequired)
                    {                        
                        //
                        // If the field was required, let's see if we can
                        // fix it by getting it from the original work item
                        //
                        if (srcWorkItem.Fields.Contains(error.Name))
                        {
                            //
                            // Map fields
                            //                            
                            destWorkItem.Fields[error.Name].Value = srcWorkItem.Fields[error.Name].Value;
                        }
                    }
                }

                //
                // Validate again
                //
                errors = destWorkItem.Validate();

                //
                // If we still have errors, show them
                //
                Console.WriteLine("remaining errors:");
                foreach (Field error in errors)
                {
                    Console.WriteLine(error.Name);
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToString());
            }
        }
    }
}